package com.itheima.health.controller.admin;

import com.itheima.health.common.MessageConst;
import com.itheima.health.result.Result;
import com.itheima.health.util.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class CommonController {
    @Autowired
    private AliOssUtil aliOssUtil;

    @PostMapping("/upload")
    public Result uplod(MultipartFile imgFile)  {
        log.info("上传的文件是：{}",imgFile);
        //获取原始文件名
        String originalFilename = imgFile.getOriginalFilename();
        //获取文件的最后点后的字符
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
       //拼接兹议定文件民和后座名
        String name = UUID.randomUUID().toString() + substring;
        try {
            String filepath =  aliOssUtil.upload(imgFile.getBytes(),name);
            return new Result(true,MessageConst.PIC_UPLOAD_SUCCESS,filepath);
        } catch (IOException e) {
           log.info("上传文件失败");
        }
        return  new Result(false, MessageConst.PIC_UPLOAD_FAIL);
    }
}
